<?php 
/* P3(c)

******************************************************************************
                                                                                      
                         __                                                        
                        /__)                                                       
                       /__        P3 - moving Technologie            
                        __)                                                          
                        __)                                                          
                                                                                        
                                                                                        
       Copyright 2009 by p3.co.at                                                                                 
                                                                                        
       visit www.p3.co.at to get more Informations 
       about Teriko - Betriebsdatenerfassung                                                                                 
       - it's not allowed to change this scripts
       - it's not allowed to remove this copyright-tag

       -- you can download the complete license   
          under http://www.teriko.org

 (c) by Patrik Pfaffenbauer and P3 (http://www.p3.co.at)

 | Programname: P3.Teriko
 | Authtor: P3.Verein
 | Coder: Patrik Pfaffenbauer
 | Version: 0.1

 | Change Index
 |_____________________________________________
 |                                             |
 |4.3.2010 Erstellung | Patrik Pfaffenbauer    |
 |_____________________________________________|

******************************************************************************

P3(c) */

class Pause extends Base
{
	public function Pause()
	{
		$this->title = $GLOBALS['lang']->GetLanguage("Pause", "title");
		$this->toolbar = true;
		$this->moduleID = 150;
		$this->permission = GetPermission($this->moduleID);
		$this->toolbarClass = "icon-48-pause";
		
		if(PermissionCount($this->permission) == 0)
		{
			$this->allowed = false;
		}
	}
	
	private function CheckInput()
	{
		include("./_tables.php");
		if($_POST['pauseName'] == "" || $_POST['pauseCode'] == "")
		{
			$this->errorText = $GLOBALS['lang']->GetLanguage("Panel", "missedMandatoryField");
			return false;
		}
		
		$query = "SELECT * FROM ".$tblpause." WHERE pauseCode='".$_POST['pauseCode']."'";
		if($GLOBALS['db']->Query($query)->num_rows)
		{
			$res = $GLOBALS['db']->Fetch($GLOBALS['db']->Query($query));
				
			if($_POST['id'] != $res->id)
			{
				$string = $GLOBALS['lang']->GetLanguage("Panel", "valueAlreadyExist");
				$string = str_replace("{0}", $GLOBALS['lang']->GetLanguage("Pause", "pauseCode"), $string);
				$string = str_replace("{1}", $_POST['pauseCode'], $string);
				
				$this->errorText = $string;
				return false;
			}
		}
		
		$query = "SELECT * FROM ".$tblpause." WHERE pauseName='".$_POST['pauseName']."'";
		if($GLOBALS['db']->Query($query)->num_rows)
		{
			$res = $GLOBALS['db']->Fetch($GLOBALS['db']->Query($query));
				
			if($_POST['id'] != $res->id)
			{
				$string = $GLOBALS['lang']->GetLanguage("Panel", "valueAlreadyExist");
				$string = str_replace("{0}", $GLOBALS['lang']->GetLanguage("Common", "description"), $string);
				$string = str_replace("{1}", $_POST['pauseName'], $string);
				
				$this->errorText = $string;
				return false;
			}
		}
		
		return true;
	}
	public function CheckFastEditInput()
	{
		include("./_tables.php");
		
		if($_GET['field'] == "pauseCode")
		{
			$query = "SELECT * FROM ".$tblpause." WHERE pauseCode='".$_GET['value']."'";
			if($GLOBALS['db']->Query($query)->num_rows)
			{
				$res = $GLOBALS['db']->Fetch($GLOBALS['db']->Query($query));
					
				if($_GET['id'] != $res->id)
				{
					$string = $GLOBALS['lang']->GetLanguage("Panel", "valueAlreadyExist");
					$string = str_replace("{0}", $GLOBALS['lang']->GetLanguage("Pause", "pauseCode"), $string);
					$string = str_replace("{1}", $_GET['value'], $string);
					
					echo "//<script>
					alert('".$string."');
					Redirect('index.php?s=pause');
					//</script>";
					return false;
				}
			}
		}
		else if($_GET['field'] == "pauseName")
		{
			$query = "SELECT * FROM ".$tblpause." WHERE pauseName='".$_GET['value']."'";
			if($GLOBALS['db']->Query($query)->num_rows)
			{
				$res = $GLOBALS['db']->Fetch($GLOBALS['db']->Query($query));
					
				if($_GET['id'] != $res->id)
				{
					$string = $GLOBALS['lang']->GetLanguage("Panel", "valueAlreadyExist");
					$string = str_replace("{0}", $GLOBALS['lang']->GetLanguage("Common", "description"), $string);
					$string = str_replace("{1}", $_GET['value'], $string);
					
					echo "//<script>
					alert('".$string."');
					Redirect('index.php?s=pause');
					//</script>";
					return false;
				}
			}
		}
		
		return true;
	}
	
	
	public function Add()
	{		
		include("./_tables.php");
		$panel = new Panel();
	
		if($this->error)
		{
			$panel->AddRow();
			$panel->AddText($this->errorText);
		}
		
		$panel->AddRow();
		$panel->AddMandatoryTextBox("pauseCode", $GLOBALS['lang']->GetLanguage("Pause", "pauseCode"), $_POST['pauseCode'], 45, 30);
		$panel->AddRow();
		$panel->AddMandatoryTextBox("pauseName", $GLOBALS['lang']->GetLanguage("Common", "description"), $_POST['pauseName'], 45, 30);
		$panel->AddRow();
		
		$panel->AddHidden("action", "save");
		$panel->AddHidden("option", "pause");
		
		$panel->Close();
	}
	public function Edit()
	{		
		include("./_tables.php");
		
		$res = $GLOBALS['db']->Fetch($GLOBALS['db']->Query("SELECT * FROM ".$tblpause." WHERE id=".$_POST['id'].""));
		
		$panel = new Panel();
		
		if(isset($_POST['pauseCode']))
		{
			if($this->error)
			{
				$panel->AddRow();
				$panel->AddText($this->errorText);
			}
			
			$res->pauseCode = $_POST['pauseCode'];
			$res->pauseName = $_POST['pauseName'];
		}

		$panel->AddRow();
		$panel->AddMandatoryTextBox("pauseCode", $GLOBALS['lang']->GetLanguage("Pause", "pauseCode"), $res->pauseCode, 45, 30);
		$panel->AddRow();
		$panel->AddMandatoryTextBox("pauseName", $GLOBALS['lang']->GetLanguage("Common", "description"), $res->pauseName, 45, 30);
		$panel->AddRow();
		
		$panel->AddHidden("action", "update");
		$panel->AddHidden("option", "pause");
		$panel->AddHidden("id", $_POST['id']);
		
		$panel->Close();
	}
	
	public function AnalysPost()
	{
		include("./_tables.php");
		$this->filter = $_POST['search'];
					
		if($_POST['action'] == "add")
		{
			$this->Add();
		}
		else if($_POST['action'] == "edit")
		{
			$this->Edit();
		}
		else if($_POST['action'] == "search")
		{
			$this->Paint();
		}
		else if($_POST['action'] == "paint")
		{
			$this->filter = "";
			$this->Paint();
		}
		else if($_POST['action'] == "save")
		{
			if($this->CheckInput())
			{
				$GLOBALS['db']->SaveObject($tblpause);
				$this->Paint();
				$this->error = false;
			}
			else
			{
				$this->error = true;
				$this->Add();
			}
		}
		else if($_POST['action'] == "update")
		{
			if($this->CheckInput())
			{
				$GLOBALS['db']->UpdateObject($tblpause);
				$this->Paint();
				$this->error = false;
			}
			else
			{
				$this->error = true;
				$this->Edit();
			}
		}
		else if($_POST['action'] == "delete")
		{
			$GLOBALS['db']->Delete($tblpause);
			$this->Paint();
		}
		else
		{
			$this->Paint();
		}
	}

	public function Paint()
	{
		include("./_tables.php");
		
		echo 
		'<div id="cpanel">';
				
		$dgv = new DataGridView($this);
		$dgv->OpenHeader();
		$dgv->AddHeader($GLOBALS['lang']->GetLanguage("Pause", "pauseCode"), "17%", true);
		$dgv->AddHeader($GLOBALS['lang']->GetLanguage("Common", "description"), "17%", true);
		$dgv->AddHeader("", "90%", true);
		$dgv->CloseHeader();
		
		$res = $GLOBALS['db']->Query("SELECT * FROM ".$tblpause);
		
		if($this->permission->editPermission)
			$title = "fastEdit";
		else
			$title = "";
		
		while($line = $GLOBALS['db']->Fetch($res))
		{
			$dgv->AddRow($line->id);
			$dgv->AddData($line->pauseCode, $title, "", "pauseCode");
			$dgv->AddData($line->pauseName, $title, "", "pauseName");
			$dgv->AddData("", "", "", "");
			
			$dgv->CloseRow();
		}
		
		$dgv->CloseDataGrid();
		
		echo '</div>';
	}
	public function Toolbar()
	{	
		if(!$this->allowed)
		{
			return;
		}
		
			
		PaintDefaultToolbar($this);
	}
	
	public function AddCSS()
	{
			
	}
	public function AddJS()
	{
		
	}
}

?>